# Wartime Page Reports.
from django.shortcuts import render_to_response
from django.template.context import RequestContext

from Wartime.wartime.models import Chieftain, CharacterClass, Clan, Empire, Warrior, Item

from pdb import set_trace as debug

class Page:
    def __init__(self, name = None, template = None, href = None):
        self.name = name
        self.template = template
        self.href = href or name

    def __repr__(self):
        return '<%s:%s:%s>' % (self.name, self.template, self.href)

    class Set(dict):
        # WebOb
        class Links(object):
            def __init__(self, pageset, player_id):
                self.__pageset = pageset
                self.__player_id = int(player_id)
            def player_id(self):
                return self.__player_id
            def __getitem__(self, name):
                return self.__pageset.url(self.__player_id, name)

        def __init__(self, *values):
            for v in values:
                if len(v) == 3:
                    (name, template, href) = v
                else:
                    (name, template) = v
                    href = name

                page = Page(name, template, href)
                self[name] = page
                self[href] = page

        base_path = 'wartime'

        def url(self, player_id, name):
            return '/%s/%d/%s' % (self.base_path, player_id, self[name].href)

        def getLinks(self, *args, **kwd):
            return self.Links(self, *args, **kwd)

        def getAnchors(self):
            return (page.href for page in self.itervalues())

        def render(self, request, template, **values):
            if 'links' not in values:
                values['links'] = self.getLinks(values['chieftain'].id)

            return render_to_response(template,
                    context_instance = RequestContext(request, values))

SitePages = Page.Set(('home',              'home.html'),
                     ('bank',              'bank.html'),
                     ('quests',            'quests.html'),
                     ('bounties',          'bounty.html'),
                     ('empire',            'empire.html'),
                     ('hospital',          'hospital.html'),
                     ('offers',            'offerings.html'),
                     ('battle_opponents',  'battle_opponents.html', 'battle/opponents'),
                     ('battle',            'battle_plan.html'),
                     ('inventory',         'inventory.html'),
                     ('alliances',         'alliances.html'),
                     ('rivals',            'rivals.html'))

def render(request, player_id = None, page_name = None):
    page_name = page_name or 'home'
    player_id = int(player_id)
    chieftain = Chieftain.objects.get(id = player_id)
    return SitePages.render(request, SitePages[page_name].template,
                            chieftain = chieftain)

def login_page(request, *args):
    reqcx = RequestContext(request, dict(chieftains = Chieftain.objects.all()))
    return render_to_response('login.html', context_instance = reqcx)

def create_chieftain(request):
    import django
    data = request.POST

    try:
        name = data['name']
        chclass = data['chclass']

    except django.utils.datastructures.MultiValueDictKeyError:
        return django.http.HttpResponseRedirect('/wartime')

    # XXX Todo: verify that chieftain isn't already created!

    chclass = CharacterClass(name = chclass)
    chclass.save()

    # This hack just creates and saves new rows for every creation!
    clan = Clan()
    empire = Empire()
    warriors = Warrior()
    items = Item()

    clan.save()
    empire.save()
    warriors.save()
    items.save()

    chieftain = Chieftain(name = name,
                          chclass = chclass,
                          max_health = 100,
                          health = 100,
                          max_energy = 10,
                          energy = 10,
                          max_stamina = 3,
                          stamina = 3,
                          level = 1,
                          experience = 9,
                          gold = 2370,
                          bank = 0,
                          clan = clan,
                          empire = empire,
                          land = 0,
                          warriors = warriors,
                          items = items,
                          favor_points = 0)

    chieftain.save()

    # The response page extends the home page.  Should it redirect?
    return SitePages.render(request, 'chieftain_created.html', chieftain = chieftain)
